package com.halflife.framework.generator;


import com.halflife.framework.generator.annotation.QueryAnn;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by zhaoyf on 2018/2/4.
 */
public class GenerateDDL {
    private Class sourceClass;
    private String databaseType;
    private String tableName;
    private ResourceBundle resource;
    private Map<String, Field> fieldMap;
    private List<Field> fields;


    public GenerateDDL(Class clazz, String tableName) {
        this.init(clazz,tableName,"mysql");
    }
    public GenerateDDL(Class clazz) {
        String tableName = "t_"+clazz.getSimpleName().toLowerCase();
        QueryAnn ann = (QueryAnn)clazz.getAnnotation(QueryAnn.class);
        if (ann != null&& StringUtils.isNotBlank(ann.table())) {
            tableName = ann.table();
        }
        this.init(clazz, tableName,"mysql");
    }
    public void init(Class clazz, String tableName, String databaseType) {
        this.sourceClass = clazz;
        this.tableName = tableName;
        this.databaseType = databaseType;
        this.resource = ResourceBundle.getBundle("templates/generator-"+this.databaseType);
        this.fieldMap = new HashMap<>();
        this.fields = new ArrayList<>();
        GenerateUtil.setFieldsBySequence(clazz,fieldMap,fields);
    }

    public static void main(String[] args) {
//        GenerateDDL ddl = new GenerateDDL(ProductType.class,"t_ProductType");
//        System.out.println(ddl.createTable());
    }
    private String getDbType(String field){
        return resource.getString("type."+field);
    }
    private String getDbLength(String field){
        if("bigdecimal".equals(field)||"double".equals(field)){
            return  resource.getString("type."+field+".length")+","+ resource.getString("type."+field+".scale");
        }
        return resource.getString("type."+field+".length");
    }

    public String createTable() {
        StringBuilder sql = new StringBuilder();
        sql.append("drop table if exists ").append(this.tableName).append(";");
        sql.append("CREATE TABLE `" + this.tableName + "` (");
        sql.append("`id` int(18) NOT NULL AUTO_INCREMENT,");
        for (Field field : fields) {
            String fieldName = field.getName();
            if (field.getType().getPackage().getName().indexOf("com.halflife") > -1) {
                continue;
            }
            String typeName = field.getType().getSimpleName().toLowerCase();
            if ("id".equals(fieldName)) {
                continue;
            } if ("companies".equals(fieldName)) {
                continue;
            }
            sql.append("`"+fieldName+"` "+this.getDbType(typeName)+"("+this.getDbLength(typeName)+") DEFAULT NULL COMMENT '"+this.sourceClass.getSimpleName()+","+fieldName+"',");

        }
        sql.append("  PRIMARY KEY (`id`)");
        sql.append(") ENGINE=MyISAM DEFAULT CHARSET=utf8;");
        System.out.println(sql.toString());
        return sql.toString();
    }
    public String updateTable() {
        StringBuilder sql = new StringBuilder();
        sql.append("alter TABLE `" + this.tableName + "` ");
        for (Field field : fields) {
            String fieldName = field.getName();
            if (field.getType().getPackage().getName().indexOf("com.halflife") > -1) {
                continue;
            }
            String typeName = field.getType().getSimpleName().toLowerCase();
            if ("id".equals(fieldName)) {
                continue;
            }
            try {
                sql.append(" add `"+fieldName+"` "+this.getDbType(typeName)+"("+this.getDbLength(typeName)+") DEFAULT NULL COMMENT '"+this.sourceClass.getSimpleName()+","+fieldName+"',\n");
            } catch (Exception e) {
//                e.printStackTrace();
            }

        }
        System.out.println(sql.toString());
        return sql.toString();
    }
}
